using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using Microsoft.ApplicationBlocks.Data;
namespace Customs.Components.SXXK.TOKHAI
{
    public class ToKhaiMauDich
    {
       
        public static string[] GetDanhSachToKhai(string maHaiQuan, string maDoanhNghiep,short namdk)
        {
            string connectionString = ConnectionManager.GetConnectionString("SXXK", maHaiQuan);
            string query = "SELECT * from DTOKHAIMD where MA_HQ=@MA_HQ and MA_DV=@MA_DV";
            if (namdk > 0)
                query += " and namdk=" + namdk;

            SqlParameter[] parameters = new SqlParameter[2];

            parameters[0] = new SqlParameter("@Ma_HQ", SqlDbType.Char);
            parameters[0].Size = 6;
            parameters[0].Direction = ParameterDirection.Input;
            parameters[0].Value = maHaiQuan;

            parameters[1] = new SqlParameter("@Ma_DV", SqlDbType.VarChar);
            parameters[1].Size = 14;
            parameters[1].Direction = ParameterDirection.Input;
            parameters[1].Value = maDoanhNghiep;

            DataSet ds = SqlHelper.ExecuteDataset(connectionString, CommandType.Text, query, parameters);
            DataSet dsHang = HangMauDich.GetDanhSachHang(maHaiQuan, maDoanhNghiep,namdk);
            string[] stXML = new string[6];
            stXML[0] = ds.GetXml();
            stXML[1] = dsHang.GetXml();
            stXML[2] = ds.GetXmlSchema();
            stXML[3] = dsHang.GetXmlSchema();
            DataSet dsPhanLuong=GetPhanLuongToKhai(maHaiQuan,maDoanhNghiep,namdk);
            stXML[4] = dsPhanLuong.GetXml();
            stXML[5] = dsPhanLuong.GetXmlSchema();
            return stXML;
        }
        public static string[] GetToKhai(string maHaiQuan, string maloaihinh, int sotk, short namdk)
        {
            string connectionString = ConnectionManager.GetConnectionString("SXXK", maHaiQuan);
            string query = "SELECT * from DTOKHAIMD where MA_HQ=@MA_HQ and MA_LH=@MA_LH AND NAMDK=@NAMDK AND SOTK=@SOTK";

            SqlParameter[] parameters = new SqlParameter[4];

            parameters[0] = new SqlParameter("@Ma_HQ", SqlDbType.Char);
            parameters[0].Size = 6;
            parameters[0].Direction = ParameterDirection.Input;
            parameters[0].Value = maHaiQuan;

            parameters[1] = new SqlParameter("@MA_LH", SqlDbType.Char);
            parameters[1].Size = 5;
            parameters[1].Direction = ParameterDirection.Input;
            parameters[1].Value = maloaihinh;

            parameters[2] = new SqlParameter("@SOTK", SqlDbType.Int);        
            parameters[2].Value = sotk;

            parameters[3] = new SqlParameter("@NAMDK", SqlDbType.SmallInt);            
            parameters[3].Value = namdk;

            DataSet ds=SqlHelper.ExecuteDataset(connectionString, CommandType.Text, query, parameters);
            DataSet dsHang = HangMauDich.GetHangOfToKhai(maHaiQuan, maloaihinh, sotk, namdk);
            string[] stXML = new string[5];
            stXML[0] = ds.GetXml();
            stXML[1] = dsHang.GetXml();
            stXML[2] = ds.GetXmlSchema();
            stXML[3] = dsHang.GetXmlSchema();
            stXML[4] = GetPhanLuongToKhai(maHaiQuan, maloaihinh, sotk, namdk);
            return stXML;
        }
        public static string GetPhanLuongToKhai(string maHaiQuan, string maloaihinh, int sotk, short namdk)
        {
            string connectionString = ConnectionManager.GetConnectionString("SLXNK", maHaiQuan);
            string query = "SELECT KetquaPL from RR_GHINHANKQ where MA_HQ=@MA_HQ and MA_LH=@MA_LH AND NAMDK=@NAMDK AND SoTK=@SOTK";

            SqlParameter[] parameters = new SqlParameter[4];

            parameters[0] = new SqlParameter("@Ma_HQ", SqlDbType.Char);
            parameters[0].Size = 6;
            parameters[0].Direction = ParameterDirection.Input;
            parameters[0].Value = maHaiQuan;

            parameters[1] = new SqlParameter("@MA_LH", SqlDbType.Char);
            parameters[1].Size = 5;
            parameters[1].Direction = ParameterDirection.Input;
            parameters[1].Value = maloaihinh;

            parameters[2] = new SqlParameter("@SOTK", SqlDbType.Int);
            parameters[2].Value = sotk;

            parameters[3] = new SqlParameter("@NAMDK", SqlDbType.SmallInt);
            parameters[3].Value = namdk;

            object o = SqlHelper.ExecuteScalar(connectionString, CommandType.Text, query, parameters);
            if (o == null)
                return null;
            return o.ToString();
        }
        public static DataSet GetPhanLuongToKhai(string maHaiQuan, string madv,short namdk)
        {
            string connectionString = ConnectionManager.GetConnectionString("SLXNK", maHaiQuan);
            string query = "SELECT SoTK,Ma_LH,Ma_HQ,NamDK,KetquaPL from RR_GHINHANKQ where MA_HQ=@MA_HQ and Ma_DV=@Ma_DV ";
            if (namdk > 0)
                query += " and namdk=" + namdk;
            SqlParameter[] parameters = new SqlParameter[2];

            parameters[0] = new SqlParameter("@Ma_HQ", SqlDbType.Char);
            parameters[0].Size = 6;
            parameters[0].Direction = ParameterDirection.Input;
            parameters[0].Value = maHaiQuan;

            parameters[1] = new SqlParameter("@Ma_DV", SqlDbType.VarChar);
            parameters[1].Size = 14;
            parameters[1].Direction = ParameterDirection.Input;
            parameters[1].Value = madv;           

            return SqlHelper.ExecuteDataset(connectionString, CommandType.Text, query, parameters);           
        }
    }
}
